package utils

import (
	"fmt"
	"local.public/innerpkg/logger"
	"math/rand"
	"strconv"
	"strings"
	"time"
)

var r *rand.Rand

func init() {
	r = rand.New(rand.NewSource(time.Now().Unix()))
}

// 指定长度的随机字符串
func RandString(len int) string {
	bytes := make([]byte, len)
	for i := 0; i < len; i++ {
		b := r.Intn(26) + 97
		bytes[i] = byte(b)
	}
	return string(bytes)
}

// 字符串转 uint
func String2Uint(str string) uint {
	num, err := strconv.ParseUint(str, 10, 32)
	if err != nil {
		return 0
	}
	return uint(num)
}

func Float2String(f float64) string {
	sResult := fmt.Sprintf("%f", f)
	// 去除result末尾的0
	for strings.HasSuffix(sResult, "0") {
		sResult = strings.TrimSuffix(sResult, "0")
	}
	if strings.HasSuffix(sResult, ".") {
		sResult = strings.TrimSuffix(sResult, ".")
	}
	return sResult
}

// Uint2String 转字符串转
func Uint2String(num uint) string {
	return fmt.Sprintf("%d", num)
}

func InStringArr(val string, array []string) bool {
	for i, _ := range array {
		if array[i] == val {
			//logs.Debug("inarray:", i, ok)
			return true
		}
	}
	return false
}

func InIntArr(val int, array []int) bool {
	for i, _ := range array {
		if array[i] == val {
			return true
		}
	}
	return false
}

func IntArrContainAny(anyValArr []int, destArray []int) bool {
	for _,val := range anyValArr {
		if InIntArr(val,destArray) {
			return true
		}
	}
	return false
}

func JoinIntArr(slice []int, sep string) string {
	elements := make([]string, len(slice))
	for i, v := range slice {
		elements[i] = strconv.Itoa(v) // 将整数转换为字符串
	}
	return strings.Join(elements, sep) // 使用strings.Join连接字符串切片
}

func PrefixInStringArr(val string, array []string) bool {
	for i, _ := range array {
		if strings.HasPrefix(val, array[i]) {
			return true
		}
	}
	return false
}

func SomeInStringArr(val string, array []string) bool {
	for i, _ := range array {
		if strings.Contains(val, array[i]) {
			return true
		}
	}
	return false
}

func LastString(ss []string) string {
	return ss[len(ss)-1]
}

func FillChar(ch string, width int) string {

	s := ""
	for len(s) < width {
		s += ch
	}
	if len(s) > width {
		s = s[:width]
	}
	return s
}
func PadLeft(source string, width int) string {
	//lenSource := len(source)
	//runeSource := []rune(source)
	//lenRuneSource := len(runeSource)
	//logger.Debug(fmt.Sprintf("lenSource is %d,lenRuneSource is %d", lenSource, lenRuneSource))

	s := source
	for len(s) < width {
		s = " " + s
	}
	if len(s) > width {
		s = s[:width]
	}
	return s
}

func PadLeftWithChinese(source string, width int) string {
	lenSource := len(source)
	runeSource := []rune(source)
	lenRuneSource := len(runeSource)
	logger.Debug(fmt.Sprintf("lenSource is %d,lenRuneSource is %d", lenSource, lenRuneSource))
	currentLen := lenSource - (lenSource-lenRuneSource)/2
	logger.Debug(fmt.Sprintf("width is %d,currentLen is %d", width, currentLen))
	if currentLen == width {
		return source
	}
	if currentLen < width {
		lenFill := width - currentLen
		logger.Debug(fmt.Sprintf("lenFill is %d", lenFill))
		return FillChar(" ", lenFill) + source
	} else {
		tmpStr := ""
		tmpLen := 0
		for _, runeChar := range runeSource {
			if runeChar > 127 { //说明是中文
				if tmpLen > width-2 {
					return tmpStr
				}
				tmpLen += 2
				tmpStr += string(runeChar)
			} else {
				if tmpLen > width-1 {
					return tmpStr
				}
				tmpLen += 1
				tmpStr += string(runeChar)
			}
		}
	}
	s := source
	for len(s) < width {
		s = " " + s
	}
	if len(s) > width {
		s = s[:width]
	}
	return s
}

func PadRight(source string, width int) string {
	//lenSource := len(source)
	//runeSource := []rune(source)
	//lenRuneSource := len(runeSource)
	//logger.Debug(fmt.Sprintf("lenSource is %d,lenRuneSource is %d", lenSource, lenRuneSource))

	s := source
	for len(s) < width {
		s += " "
	}
	if len(s) > width {
		s = s[:width]
	}
	return s
}

func PadRightWithChinese(source string, width int) string {
	lenSource := len(source)
	runeSource := []rune(source)
	lenRuneSource := len(runeSource)
	//logger.Debug(fmt.Sprintf("lenSource is %d,lenRuneSource is %d", lenSource, lenRuneSource))

	currentLen := lenSource - (lenSource-lenRuneSource)/2
	//logger.Debug(fmt.Sprintf("width is %d,currentLen is %d", width, currentLen))
	if width == currentLen {
		return source
	}
	if currentLen < width {
		lenFill := width - currentLen
		//logger.Debug(fmt.Sprintf("lenFill is %d", lenFill))
		return source + FillChar(" ", lenFill)
	} else {
		tmpStr := ""
		tmpLen := 0
		for _, runeChar := range runeSource {
			if runeChar > 127 { //说明是中文
				if tmpLen > width-2 {
					return tmpStr
				}
				tmpLen += 2
				tmpStr += string(runeChar)
			} else {
				if tmpLen > width-1 {
					return tmpStr
				}
				tmpLen += 1
				tmpStr += string(runeChar)
			}
		}
	}
	s := source
	for len(s) < width {
		s += " "
	}
	if len(s) > width {
		s = s[:width]
	}
	return s
}

func PadBoth(source string, width int) string {
	//lenSource := len(source)
	//runeSource := []rune(source)
	//lenRuneSource := len(runeSource)
	//logger.Debug(fmt.Sprintf("lenSource is %d,lenRuneSource is %d", lenSource, lenRuneSource))

	s := source
	for len(s) < width {
		s = " " + s + " "
	}
	if len(s) > width {
		s = s[:width]
	}
	return s
}
